package sample.tools;

/**
 * 作者：w on 2021-08-06.
 * 邮箱：y141111@126.com
 * 版本：v1.0
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class TCPClient {
    Socket client;
    InputStream inputStream;
    OutputStream outputStream;
    public boolean isConnTCP = false;

    /**
     * 连接TCP
     * @param ip ip
     * @param port port
     * @return 是否连接成功
     */
    public boolean conn(String ip,Integer port) {
        try{
            client = new Socket();
            client.connect(new InetSocketAddress(ip,port),1500);
            client.setSoTimeout(2000);
            isConnTCP = true;
        }catch (Exception eee){
            isConnTCP = false;
        }
        return isConnTCP;
    }

    /**
     * 断开连接TCP
     */
    public void close() {
        try{
            client.close();
            isConnTCP = false;
        }catch (Exception ignored){

        }
    }

    /**
     * 接收数据
     * @return 返回byte[]类型数据
     */
    public byte[] recover(){
        byte[] strRecover;
        if (isConnTCP){
            try {
                inputStream = client.getInputStream();
                byte[] data = new byte[1024];
                int len = inputStream.read(data);
                strRecover = new byte[len];
                System.arraycopy(data, 0, strRecover, 0, len);
            }catch (SocketTimeoutException ste){
                strRecover = "读取超时".getBytes();
            }catch (SocketException se){
                isConnTCP = false;
                strRecover = ("出现异常:" + se.toString()).getBytes();
            }catch (NegativeArraySizeException nase){   // 服务端断开连接抛出的异常
                isConnTCP = false;
                strRecover = "请求重连".getBytes();
            }catch (Exception e){
                strRecover = ("出现异常:" + e.toString()).getBytes();
            }
        }else {
            strRecover = "未连接".getBytes();
        }
        return strRecover;
    }

    /**
     * 发送数据
     * @param str 要发送的string
     * @return 是否发送成功
     */
    public String send(String str){
        String strSend = "";
        if (isConnTCP){
            try {
                outputStream = client.getOutputStream();
                outputStream.write(str.getBytes());
                strSend = "发送成功!";
            }catch (SocketException se){
                strSend = "出现异常:" + se.toString();
                isConnTCP = false;
            }catch (Exception e){
                strSend = "出现异常" + e.toString();
            }
        }else {
            strSend = "未连接!";
        }
        return strSend;
    }

    public String sendHexMessage(byte[] message){
        String strSend = "";
        if (isConnTCP) {
            try{
                outputStream = client.getOutputStream();
                if (outputStream != null) {
                    outputStream.write(message);
                    outputStream.flush();
                    strSend = "发送成功";
                }
            }catch (IOException e){
                strSend = e.toString();
            }
        }else {
            strSend = "未连接!";
        }
        return strSend;
    }
}
